package com.small.act;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;

import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.Common;
import com.small.commom.AsyncDataLoader.Callback;
import com.smell.R;

public class UserInfoActivity extends Activity {

	EditText txt_nick, txt_name, txt_sex, txt_birth, txt_phone, txt_email,
			txt_pass, txt_newPass;
	ImageButton btn_back, btn_on;
	private int mYear;
	private int mMonth;
	private int mDay;
	private Action action;
	private int result = 0;

	private String strNick;
	private String strName;
	private String strSex;
	private String birthday;
	private String strPhone;
	private String strEmail;
	private String strPass;
	private String strNewPass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo_view);
		action = new Action(UserInfoActivity.this);
		findView();
		onData();
		Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
	}

	public void onData() {
		if (Common.model != null) {
			txt_nick.setText(Common.model.getNickName());
			txt_name.setText(Common.model.getUserName());
			txt_sex.setText(Common.model.getSex());
			txt_birth.setText(Common.model.getBirthday());
			txt_phone.setText(Common.model.getTel());
			txt_email.setText(Common.model.getEmail());
		} else {
			action.showLongToast("没有个人信息,请重新登录...");
			finish();
		}
	}

	public void findView() {
		txt_nick = (EditText) this.findViewById(R.id.userdetail_txt_nickname);
		txt_name = (EditText) this.findViewById(R.id.userdetail_txt_name);
		txt_sex = (EditText) this.findViewById(R.id.userdetail_txt_sex);
		txt_birth = (EditText) this.findViewById(R.id.userdetail_txt_bothday);
		txt_phone = (EditText) this.findViewById(R.id.userdetail_txt_phone);
		txt_email = (EditText) this.findViewById(R.id.userdetail_txt_email);
		txt_pass = (EditText) this.findViewById(R.id.userdetail_txt_pass);
		txt_newPass = (EditText) this.findViewById(R.id.userdetail_txt_newpass);
		btn_back = (ImageButton) this.findViewById(R.id.btn_back);
		btn_on = (ImageButton) this.findViewById(R.id.userdetail_btn_on);

		btn_back.setOnClickListener(onClick);
		btn_on.setOnClickListener(onClick);
		txt_sex.setOnClickListener(onClick);
		txt_birth.setOnClickListener(onClick);
	}

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btn_back:// 返回
				finish();
				break;
			case R.id.userdetail_btn_on:// 提交
				onUpload();
				break;
			case R.id.userdetail_txt_sex:// 性别
				setSex();
				break;
			case R.id.userdetail_txt_bothday:// 提交
				setBirthDaty();
				break;
			}
		}
	};

	public void onUpload() {
		strNick = txt_nick.getText().toString().trim();
		strName = txt_name.getText().toString().trim();
		strSex = txt_sex.getText().toString().trim();
		birthday = txt_birth.getText().toString().trim();
		strPhone = txt_phone.getText().toString().trim();
		strEmail = txt_email.getText().toString().trim();
		strPass = txt_pass.getText().toString().trim();
		strNewPass = txt_newPass.getText().toString().trim();

		if (strNick.equals("")) {
			txt_nick.setError("请输入");
			return;
		}
		if (strName.equals("")) {
			txt_name.setError("请输入");
			return;
		}
		if (strSex.equals("")) {
			txt_sex.setError("请输入");
			return;
		}
		if (birthday.equals("")) {
			txt_birth.setError("请输入");
			return;
		}
		if (strPhone.equals("")) {
			txt_phone.setError("请输入");
			return;
		}
		if (strEmail.equals("")) {
			txt_email.setError("请输入");
			return;
		} else if (!isEmail(strEmail)) {
			txt_email.setError("邮箱输入错误");
			return;
		}

		action.showProgressDialog("正在提交...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	protected void setSex() {
		final CharSequence[] items = { "女", "男" };
		AlertDialog dlg = new AlertDialog.Builder(UserInfoActivity.this)
				.setTitle("请选择")
				.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						txt_sex.setText(items[item]);
					}
				}).create();
		dlg.show();
	}

	protected void setBirthDaty() {
		Dialog d = new DatePickerDialog(UserInfoActivity.this,
				new OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						// TODO Auto-generated method stub
						mYear = year;
						mMonth = monthOfYear;
						mDay = dayOfMonth;
						txt_birth.setText(new StringBuilder()
								.append(mYear)
								.append("/")
								.append((mMonth + 1) < 10 ? "0" + (mMonth + 1)
										: (mMonth + 1)).append("/")
								.append((mDay < 10) ? "0" + mDay : mDay));
					}

				}, mYear, mMonth, mDay);
		d.show();

	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {

			result = action.modifyUserInfo(Common.model.getMemberID(), strNick,
					strPhone, strSex, birthday, strEmail);
		}

		@Override
		public void onPrepare() {
		}

		@Override
		public void onFinish() {
			action.stopProgressDialog();
			modifyResult(result);
		}
	};

	protected void modifyResult(int result2) {
		switch (result2) {

		case 0:
			action.showShortToast("提交失败，请稍后再试...");
			break;
		case 1:
			action.showShortToast("提交成功...");

			break;

		case -2:
			action.showShortToast("网络异常，请稍候重试...");
			break;
		default:
			action.showShortToast("提交失败，请稍后再试...");
			break;
		}
	}

	public static boolean isEmail(String strEmail) {
		Pattern p = Pattern
				.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		Matcher m = p.matcher(strEmail);
		return m.matches();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		action.stopProgressDialog();

	}
}
